package common

import (
	"strings"
	"time"
)

type BaseModel struct {
	Id        uint       `json:"id" uri:"id" gorm:"primary_key"`
	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"deleted_at" sql:"index"`
}

type BaseSearchModel struct {
	Name       string `form:"name"`
	UserId      string `form:"user_id"  uri:"user_id"`
	TagId      string `form:"tag_id"  uri:"tag_id"`
	Page       int    `form:"page"`
	Limit      int    `form:"limit"`
	Status     string `form:"status"`
	Sort       string `form:"sort"`
	ClassifyId string `form:"classify_id"`
	CategoryId string `form:"category_id"`
}

func (baseSearchModel BaseSearchModel) GetSort() string {
	if baseSearchModel.Sort == "" {
		return ""
	}
	if strings.HasPrefix(baseSearchModel.Sort, "+") {
		sorts := strings.Split(baseSearchModel.Sort, "+")
		if len(sorts) == 2 {
			return sorts[len(sorts)-1] + " acs"
		}
	}
	if strings.HasPrefix(baseSearchModel.Sort, "-") {
		sorts := strings.Split(baseSearchModel.Sort, "-")
		if len(sorts) == 2 {
			return sorts[len(sorts)-1] + " desc"
		}
	}
	return ""
}
